package com.example.tiptravel.view.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.tiptravel.R;
import com.example.tiptravel.commom.utils.ImageLoader;
import com.example.tiptravel.entity.home.spot.SpotEntity;

import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 45820 on 2016/11/10.
 */

public class SpotAdapter extends RecyclerView.Adapter<SpotAdapter.SpotViewHolder> {

    private List<SpotEntity.DataBean> dataBeen;
    private Context context;

    public SpotAdapter(List<SpotEntity.DataBean> dataBeen, Context context) {
        this.dataBeen = dataBeen;
        this.context = context;
    }

    @Override
    public SpotViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SpotViewHolder(LayoutInflater.from(context).inflate(R.layout.item_list_spot, parent, false));
    }

    @Override
    public void onBindViewHolder(SpotViewHolder holder, int position) {
        ImageLoader.getInstance().displayImage(dataBeen.get(position).getPic(),context,holder.ivSpotImg,R.mipmap.bg_scenic_list_media,R.mipmap.bg_scenic_list_media);
        if (dataBeen.get(position).getAudio().size()!=0){
            holder.cbSpotPlay.setVisibility(View.VISIBLE);
            final MediaPlayer mediaPlayer = MediaPlayer.create(context, Uri.parse(dataBeen.get(position).getAudio().get(0).getMp3()));
            holder.cbSpotPlay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        mediaPlayer.start();
                    }else {
                        mediaPlayer.pause();
                    }
                }
            });
        }else {
            holder.cbSpotPlay.setVisibility(View.GONE);
        }
        holder.tvSpotName.setText(dataBeen.get(position).getScenicName());
    }

    @Override
    public int getItemCount() {
        return dataBeen != null ? dataBeen.size() : 0;
    }

    public class SpotViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_spot_img)
        ImageView ivSpotImg;
        @BindView(R.id.rb_spot_play)
        CheckBox cbSpotPlay;
        @BindView(R.id.tv_spot_name)
        TextView tvSpotName;
        @BindView(R.id.tv_spot_tips)
        TextView tvSpotTips;

        public SpotViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
